<?php
/**
 * @title 416. 分割等和子集 -
 * @author start2004
 */

ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Boolean
     */
    function canPartition($nums) {
        /**
         * @since 2020-10-11 排序
         */
        sort($nums);

        /**
         * @since 2020-10-11 总和, 正整数
         */
        $sum = array_sum($nums);
        if($sum%2 == 1){
            return false;
        } else {
            $half = $sum>>1;
        }

        /**
         * @since 2020-10-12 缓存
         */
        $this->dps = [];

        /**
         * @return
         */
        $this->nums = $nums;
        return self::find(0, $half, $sum, 0);
    }

    /**
     * @title 是否存在元素之和等于half
     * @author start2004
     * @since 2020-10-11 Created
     *
     * @param int $key
     * @param int $target
     * @param int $max
     * @param int $lastValue
     * @return bool
     */
    public function find($key = 0, $target = 0, $max = 0, $lastValue = 0){
        $k = "{$key}-{$target}-{$max}-{$lastValue}";
        if(isset($this->dps[$k])){
            return $this->dps[$k];
        } else {}

        /**
         * @since 2020-10-11 目标值等于可能的最大值, 即key和后续所有元素都选上
         */
        if($target == $max){
            $this->dps[$k] = true;
            return true;
        } elseif($target > $max) {
            /**
             * @since 2020-10-11 目标值大于可能的最大值, 即key和后续所有元素都选上也无法达到目标值
             */
            $this->dps[$k] = false;
            return false;
        }

        /**
         * @since 2020-10-11 key值
         */
        $val = $this->nums[$key];

        /**
         * @since 2020-10-11 升序关系, 如果val大于目标值, 后续每个元素都会大于目标值
         */
        if($val > $target){
            $this->dps[$k] = false;
            return false;
        } elseif($val == $target) {
            /**
             * @since 2020-10-11 达到目标值
             */
            $this->dps[$k] = true;
            return true;
        }

        /**
         * @since 2020-10-11 和上一个元素值相同, 如果上个元素无, 本元素继续无
         */
        $max = $max-$val;
        if($val === $lastValue){
        } else {
            /**
             * @since 2020-10-11 key值存在
             */
            if(self::find($key+1, $target-$val, $max, 0) === true){
                $this->dps[$k] = true;
                return true;
            } else {}
        }

        /**
         * @since 2020-10-11 key值不存在
         */
        $result = self::find($key+1, $target, $max, $val);

        /**
         * @return
         */
        $this->dps[$k] = $result;
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.11_2.php
 */
$datas = [
    [
        [4,4,4,4,4,4,4,4,8,8,8,8,8,8,8,8,12,12,12,12,12,12,12,12,16,16,16,16,16,16,16,16,20,20,20,20,20,20,20,20,24,24,24,24,24,24,24,24,28,28,28,28,28,28,28,28,32,32,32,32,32,32,32,32,36,36,36,36,36,36,36,36,40,40,40,40,40,40,40,40,44,44,44,44,44,44,44,44,48,48,48,48,48,48,48,48,52,52,52,52,52,52,52,52,56,56,56,56,56,56,56,56,60,60,60,60,60,60,60,60,64,64,64,64,64,64,64,64,68,68,68,68,68,68,68,68,72,72,72,72,72,72,72,72,76,76,76,76,76,76,76,76,80,80,80,80,80,80,80,80,84,84,84,84,84,84,84,84,88,88,88,88,88,88,88,88,92,92,92,92,92,92,92,92,96,96,96,96,96,96,96,96,97,99],
    ],
    [
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,97,95], // true
    ],
    [
        [100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,99,97], // false
    ],
    [
        [2,2,1,1], // true
    ],
    [
        [1, 5, 11, 5], // true
    ],
    [
        [1, 2, 3, 5], // false
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->canPartition(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
